﻿using ChinaUnicom.ViewModels;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;

namespace ChinaUnicom.Views
{
    public partial class Home : PhoneApplicationPage
    {
        HomeContext HomeContext { get { return DataContext as HomeContext; } }

        #region IconButtons
        Dictionary<string, ApplicationBarIconButton> ApplicationBarIconButtons = new Dictionary<string, ApplicationBarIconButton>();
        const string LoginIconKey = "LoginIconKey";
        const string InfoIconKey = "InfoIconKey";
        const string SettingsIconKey = "SettingsIconKey";
        const string SearchIconKey = "SearchIconKey";
        //const string StatisticsIconKey = "StatisticsIconKey";

        private void OnLoginIconButtonClick(object sender, EventArgs e)
        {
            if (HomeContext != null)
            {
                HomeContext.LoginCommand.Execute("OnLoginIconButtonClick");
            }
        }

        private void OnSearchIconButtonClick(object sender, EventArgs e)
        {
            if (HomeContext != null)
            {
                HomeContext.SearchCommand.Execute("OnSearchIconButtonClick");
            }
        }

        private void OnInfoIconButtonClick(object sender, EventArgs e)
        {
            if (HomeContext != null)
            {
                HomeContext.InfoCommand.Execute("OnInfoIconButtonClick");
            }
        }

        private void OnSettingsIconButtonClick(object sender, EventArgs e)
        {
            if (HomeContext != null)
            {
                HomeContext.SettingsCommand.Execute("OnSettingsIconButtonClick");
            }
        }

        private void OnStatisticsIconButtonClick(object sender, EventArgs e)
        {
            if (HomeContext != null)
            {
                HomeContext.StatisticsCommand.Execute("OnStatisticsIconButtonClick");
            }
        }

        void ApplicationBarIconButtonInitialize()
        {        
            /*ApplicationBarIconButton StatisticsIconButton = new ApplicationBarIconButton();
            StatisticsIconButton.Text = "账户统计";
            StatisticsIconButton.IconUri = new Uri("/Themes/Images/statistics.png", UriKind.Relative);
            StatisticsIconButton.Click += OnStatisticsIconButtonClick;
            ApplicationBarIconButtons.Add(StatisticsIconKey, StatisticsIconButton);*/

            ApplicationBarIconButton SearchIconButton = new ApplicationBarIconButton();
            SearchIconButton.Text = "查询";
            SearchIconButton.IconUri = new Uri("/Themes/Images/search.png", UriKind.Relative);
            SearchIconButton.Click += OnSearchIconButtonClick;
            ApplicationBarIconButtons.Add(SearchIconKey, SearchIconButton);

            ApplicationBarIconButton InfoIconButton = new ApplicationBarIconButton();
            InfoIconButton.Text = "我的信息";
            InfoIconButton.IconUri = new Uri("/Themes/Images/info.png", UriKind.Relative);
            InfoIconButton.Click += OnInfoIconButtonClick;
            ApplicationBarIconButtons.Add(InfoIconKey, InfoIconButton);

            ApplicationBarIconButton SettingsIconButton = new ApplicationBarIconButton();
            SettingsIconButton.Text = "设置";
            SettingsIconButton.IconUri = new Uri("/Themes/Images/settings.png", UriKind.Relative);
            SettingsIconButton.Click += OnSettingsIconButtonClick;
            ApplicationBarIconButtons.Add(SettingsIconKey, SettingsIconButton);

            ApplicationBarIconButton LoginIconButton = new ApplicationBarIconButton();
            LoginIconButton.Text = "登录";
            LoginIconButton.IconUri = new Uri("/Themes/Images/user-login.png", UriKind.Relative);
            LoginIconButton.Click += OnLoginIconButtonClick;
            ApplicationBarIconButtons.Add(LoginIconKey, LoginIconButton);
        }
        #endregion

        public Home()
        {
            InitializeComponent();
            ApplicationBarIconButtonInitialize();

            DataContext = new HomeContext();
            Loaded += OnPageLoaded;
        }

        void OnPageLoaded(object sender, RoutedEventArgs e)
        {
            HomeContext.Load();

            if (!Authentication.Current.Authorized)
            {
                ApplicationBarIconButtons[InfoIconKey].IsEnabled = false;  
                ApplicationBarIconButtons[SearchIconKey].IsEnabled = false;

                foreach (var button in ApplicationBarIconButtons.Select(x => x.Value))
                {
                    if (!ApplicationBar.Buttons.Contains(button))
                    {
                        ApplicationBar.Buttons.Add(button);
                    }
                }
            }
            else
            {
                ApplicationBarIconButtons[InfoIconKey].IsEnabled = true;
                ApplicationBarIconButtons[SearchIconKey].IsEnabled = true;


                if (ApplicationBar.Buttons.Contains(ApplicationBarIconButtons[LoginIconKey]))
                {
                    ApplicationBar.Buttons.Remove(ApplicationBarIconButtons[LoginIconKey]);
                }

                foreach (var button in ApplicationBarIconButtons.Where(x => x.Key != LoginIconKey).Select(x => x.Value))
                {
                    if (ApplicationBar.Buttons != null && !ApplicationBar.Buttons.Contains(button))
                    {
                        ApplicationBar.Buttons.Add(button);
                    }
                }
            }

            PivotControl.SelectionChanged -= OnContainerSelectionChanged;
            PivotControl.SelectionChanged += OnContainerSelectionChanged;
        }

        private void OnContainerSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            PivotItemType type = PivotItemType.Package;
            Pivot pivot = sender as Pivot;
            switch (pivot.SelectedIndex)
            {
                case 1:
                    type = PivotItemType.Fee;
                    break;
                case 2:
                    type = PivotItemType.FeeHitory;
                    break;
                default:
                    type = PivotItemType.Package;
                    break;
            }

            HomeContext.PivotSelectionChanged(type);
        }

        private void OnPackagePulled(object sender, EventArgs e)
        {
            HomeContext.PackagePullrefresh = "松开刷新";
        }

        private void OnPackageReleased(object sender, EventArgs e)
        {
            HomeContext.ForceUpdate(PivotItemType.Package);
            HomeContext.PackagePullrefresh = "下拉可以刷新";
        }

        private void OnFeePulled(object sender, EventArgs e)
        {
            HomeContext.FeePullrefresh = "松开刷新";
        }

        private void OnFeeReleased(object sender, EventArgs e)
        {
            HomeContext.ForceUpdate(PivotItemType.Fee);
            HomeContext.FeePullrefresh = "下拉可以刷新";
        }
    }
}